<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/12
 * Time: 9:51
 */
class GetVersionUpdateInfo extends ApiPublicBase
{
    public function logic()
    {
        $lastVersionInfo = $this->redis->get('lastVersion');
        $lastVersionInfo = json_decode($lastVersionInfo, true);

        $result = [
            'hasNewVersion' => false,
            'detail' => []
        ];

        if (isset($lastVersionInfo[$this->platform])) {
            $platformVersion = $lastVersionInfo[$this->platform];
            $versionList = $platformVersion['versionList'];

            foreach ($versionList as $row) {
                if ($row['preVersion'] == $this->ver) {
                    if ($this->platform == 1) {
                        $row['channelList'] = explode(',', $row['channelList']);
                        if (in_array($this->channelId, $row['channelList'])) {
                            $result['hasNewVersion'] = true;
                            $result['detail'] = [
                                'isForce' => $row['isForce'] ? true : false,
                                'readme' => $platformVersion['readme'],
                                'url' => $this->getChannelVersionUrl($platformVersion)
                            ];
                        }
                    } else {
                        $result['hasNewVersion'] = true;
                        $result['detail'] = [
                            'isForce' => $row['isForce'] ? true : false,
                            'readme' => $platformVersion['readme'],
                            'url' => $this->getChannelVersionUrl($platformVersion)
                        ];
                    }
                }
            }
        }

        $this->returnData('suc_action', $result);
    }

    public function getChannelVersionUrl($versionInfo)
    {
        $url = '';

        /**
         * 整包更新
         */
        if (is_numeric($versionInfo['type']) && $versionInfo['type'] == 0) {
            $this->initMysql();
            $where = [
                'version' => $versionInfo['version'],
                'channelID' => $this->channelId
            ];
            $result = $this->mysql->findRow('channel_version_detail', $where, 'file');
            $result = $result ? $result['file'] : '';
        } else {
            /**
             * 热包更新
             */
            $result = 'data/' . $this->gameId . '/v' . $this->getMainVersionID($versionInfo['version']) . '_master';
            if ($this->platform == 2) {
                $result = $result . '_ios';
            }
        }

        if ($result) {
            $url = 'http' . '://' . $_SERVER['HTTP_HOST'] . '/' . $result;
        }

        return $url;
    }

    /**
     * 获取大版本
     * @param $version
     * @return string
     */
    public function getMainVersionID($version)
    {
        return substr($version, 0, strpos($version, '.'));
    }
}